from apscheduler.schedulers.background import BackgroundScheduler, BlockingScheduler
from apscheduler.jobstores.redis import RedisJobStore
from apscheduler.executors.pool import ThreadPoolExecutor


class Task:
    def __init__(self, **kwargs):
        self.scheduler = None

        if kwargs:
            self.init_config(**kwargs)

    def init_config(self, store_configure, default_configure, pool_count=20, is_background=True):
        jobstores = {'default': RedisJobStore(**store_configure)}
        executors = {'default': ThreadPoolExecutor(pool_count)}
        if is_background:
            self.scheduler = BackgroundScheduler(jobstores=jobstores, executors=executors,
                                                 job_defaults=default_configure)
            return
        self.scheduler = BlockingScheduler(jobstores=jobstores, executors=executors, job_defaults=default_configure)


task = Task()


if __name__ == '__main__':
    task.init_config(
        default_configure={'coalesce': True, 'max_instances': 3},
        store_configure={'default': RedisJobStore(host='192.168.101.79', password='MY.io2019')}
    )
